package com.cxl.sort;

import java.util.ArrayList;
import java.util.Random;
/*
 * Location: E:\work\eclipse3.6_\workspace\design_pattern\ Qualified Name:
 * com.cxl.sort.InsertSort JD-Core Version: 0.6.1
 */
public class InsertSort {
	private ArrayList<Integer> al;

	public InsertSort(int num, int mod) {
		this.al = new ArrayList(num);
		Random rand = new Random();
		System.out.println("The ArrayList Sort Before:");
		for (int i = 0; i < num; i++) {
			this.al.add(new Integer(Math.abs(rand.nextInt()) % mod + 1));
			System.out.println("al[" + i + "]=" + this.al.get(i));
		}
	}

	public void SortIt() {
		int MaxSize = 1;
		for (int i = 1; i < this.al.size(); i++) {
			Integer tempInt = (Integer) this.al.remove(i);
			if (tempInt.intValue() >= ((Integer) this.al.get(MaxSize - 1)).intValue()) {
				this.al.add(MaxSize, tempInt);
				MaxSize++;
				System.out.println(this.al.toString());
			} else {
				for (int j = 0; j < MaxSize; j++) {
					if (((Integer) this.al.get(j)).intValue() >= tempInt.intValue()) {
						this.al.add(j, tempInt);
						MaxSize++;
						System.out.println(this.al.toString());
						break;
					}
				}
			}
		}
		System.out.println("The ArrayList Sort After:");
		for (int i = 0; i < this.al.size(); i++)
			System.out.println("al[" + i + "]=" + this.al.get(i));
	}

	public static void main(String[] args) {
		InsertSort is = new InsertSort(10, 100);
		is.SortIt();
	}
}

